// pages/recharge/recharge.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        moneyList: [{
                name: '20元',
                value: 20,
            },
            {
                name: '50元',
                value: 50,
            },
            {
                name: '100元',
                value: 100,
            },
            {
                name: '300元',
                value: 300,
            },
            {
                name: '1000元',
                value: 1000,
            },
            {
                name: '5000元',
                value: 5000,
            },
        ],
        currentMoney: null,
        balance: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.loadData()
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.post('rest/member/queryById.do').then(res => {
            this.setData({
                balance: res.obj.money ? res.obj.money : 0
            })
            wx.hideLoading()
        })
    },
    chooseMoney(evt) {
        let currentMoney = evt.currentTarget.dataset.value
        this.setData({
            currentMoney
        })
    },
    bindinput(evt) {
        let value = evt.detail.value
        this.setData({
            currentMoney: value
        })
    },
    /**
     * type 1.充值账户  2.充值积分
     */
    submit() {
        if (!this.data.currentMoney) {
            wx.showToast({
                icon: 'none',
                title: '请选择或输入充值金额',
            })
            return
        }
        let reg = /^(?!^0*(\.0{1,2})?$)^\d{1,13}(\.\d{1,2})?$/
        if (!reg.test(this.data.currentMoney)) {
            wx.showToast({
                icon: 'none',
                title: '充值金额不正确',
            })
            return
        }
        wx.showLoading({
            title: '请稍等',
        })
        getApp().http.post('rest/member/recharge.do', {
            data: {
                type: 1,
                money: this.data.currentMoney
            }
        }).then(res => {
            wx.hideLoading()
            let js = JSON.parse(res.obj)
            wx.requestPayment(Object.assign({
                success: res => {
                    wx.navigateTo({
                        url: './recharge-success?price=' + this.data.currentMoney,
                    })
                },
                fail: res => {
                    wx.showToast({
                        icon: 'none',
                        title: '充值失败,请重试',
                    })
                }
            }, js))
        })
    }
})